// pages/editAddress/editAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    default:false,    //是否为默认地址   1默认收货地址     2 不是默认收货地址 
    name:'',
    phone:'',
    address:'',
    addressid:'',
    show: false,   //删除收件地址确认的弹出层是否显示
  },
  // 提交收货信息
  editsubmit(){
    var that = this
    if(this.data.name == '' || this.data.phone == '' || this.data.address == ''){
      wx.showToast({
        title: '请输入完整信息！',
        duration: 2000,
        icon:'none'
        });
    }else if(!(/^1[34578]\d{9}$/.test(this.data.phone))){
      wx.showToast({
        title: '手机号码有误',
        duration: 2000,
        icon:'none'
        });
    }else{
      // console.log('that.data.default',that.data.default)
      var defaultprefer = that.data.default ? '1' : '2'
      console.log('defaultprefer',defaultprefer)
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/address/update',
        data: {
          addressid:that.data.addressid,
          userid:getApp().globalData.openid,
          name:that.data.name,
          phone:that.data.phone,
          address:that.data.address,
          prefer:defaultprefer
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
          // success
          // console.log('res',res)
          if(res.data.flag){
            wx.showToast({
              title: '修改成功',
              duration: 2000,
              icon:'none'
            });
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/shippingAddress/shippingAddress'
              })
            },1000);
            
          }else{
            wx.showToast({
              title: '修改失败',
              duration: 2000,
              icon:'none'
            });
          }
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
        }
      })
    }
  },
  // 删除的确定按钮，提交数据
  deletesubmit:function(){
    // 调接口
    var that = this
    console.log('that.data.addressid',that.data.addressid)
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/address/delete',
      data: {
        addressid:that.data.addressid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log('res',res)
        if(res.data.flag){
          wx.showToast({
            title: '删除成功',
            duration: 2000,
            icon:'none'
          });
          wx.navigateTo({
            url: '/pages/shippingAddress/shippingAddress'
          })
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
    this.setData({ show: false });

  },
  // 显示弹出层
  delete:function(){
    this.setData({ show: true });
  },
  // 关闭弹出层
  onClose() {
    this.setData({ show: false });
  },
  // 用户名
  inputname(event) {
    this.setData({
      name:event.detail.value
    })
  },
  // 手机号
  inputphone(event) {
    this.setData({
      phone:event.detail.value
    })
  },
  // 收获地址
  inputaddress(event) {
    this.setData({
      address:event.detail.value
    })
  },
  // 修改是否为默认收货地址
  defaultaddress:function(){
    if(!this.data.default){
      this.setData({ default: !this.data.default });
    }
    
    // console.log('default',this.data.default)
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var defaultperfer = options.prefer == 1 ? true : false    //默认收货地址
    this.setData({ 
        name: options.name,
        phone:options.phone,
        address:options.address,
        default:defaultperfer,
        addressid:options.addressid
     });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})